const db = wx.cloud.database()
//gw=1未来记录  2过去多少

Page({
  data: {
    left_time_list: [], // 用于存储剩余时间转换后的天、时、分、秒数组
    arr_time: [], //存储倒计时的数据
    datalist: [], // 初始数据
    timer:'',//定时器
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log('显示哟');
    let a  = setInterval(() => {
      this.xz()
     }, 1000)
     this.setData({
       timer:a
     })
  },
// 页面隐藏显示
  onHide() {
    console.log('关闭漏');
    if (this.data.timer) {
      clearTimeout(this.data.timer)
      console.log(this.data.timer);
      clearInterval(this.data.timer);
      this.setData({
        timer:null
      })
    }
  },
  onLoad: function () {
    this.getDaoJiShi()
    // this.data.timer = setInterval(() => {
    //  this.xz()
    // }, 1000)
    // this.data.timer.start(); // 启动定时任务
  },

  //获取数据库数据
  getDaoJiShi() {
    db.collection('daojishi').get({
      success: async res => {
        // console.log(new Date() - res.data[0].date);
        let arr = this.chuli(res.data)
        this.setData({
          arr_time: arr
        })
      }
    })
  },
  // 循环
  xz() {
    let a = this.data.arr_time
   let arr=  this.chuli(a)
   this.setData({
     arr_time:arr
   })

  },
  chuli(data) {
    let dataarr = data
    // console.log(dataarr);
    //当前时间
    let time = new Date()
    // 当前时间减去制定时间大于0就是过去了多少天 小于0未来
    for (let i = 0; i < dataarr.length; i++) {
      let element = dataarr[i]
      let pd = element.gw == 1 ? true : false
      //判断用那个函数
      if (pd) {
        //判断是不是生日
        if (element.beizhu == '生日') {
          let day = element.date.getDate()
          let month = element.date.getMonth() + 1
          let string = `${new Date().getFullYear()+1}-${month}-${day} 00:00:00`
          element.date = new Date(string)
        }
        // 未来-现在
        let arr = this.boforeTime(element.date - time)
        element.list_time = arr
      } else {
        let arr = this.boforeTime(time - element.date)
        element.list_time = arr
      }
    }

    return dataarr
  },


  boforeTime(date) {
    let chazhi = date
    // 计算天数
    const days = Math.floor(chazhi / (1000 * 60 * 60 * 24));
    // 去除已经计算过的天数对应的毫秒数，剩下的毫秒数用于计算小时
    const remainingMsAfterDays = chazhi % (1000 * 60 * 60 * 24);
    const hours = Math.floor(remainingMsAfterDays / (1000 * 60 * 60));
    // 去除已算入小时数对应的毫秒数，剩余毫秒数用于后续计算
    const remainingMsAfterHours = remainingMsAfterDays % (1000 * 60 * 60);
    // 计算分钟数
    const minutes = Math.floor(remainingMsAfterHours / (1000 * 60));
    // 去除已算入分钟数对应的毫秒数，剩余毫秒数用于计算秒数
    const remainingMsAfterMinutes = remainingMsAfterHours % (1000 * 60);
    // 计算秒数
    const seconds = Math.floor(remainingMsAfterMinutes / 1000);
    // let string = `${days}天${hours}小时${minutes}分钟${seconds}秒`
    // console.log(string);
    let arr = [days, hours, minutes, seconds]
    return arr
  },
  //页面离开执行
  onUnload: function () {
    // 在页面卸载时清除定时器
    if (this.data.timer) {
      clearTimeout(this.data.timer)
      console.log(this.data.timer);
      clearInterval(this.data.timer);
      this.data.timer = null;
    }
  }


  

})